<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 3.3</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="3.2.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="3.4.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P1">Part I. The Language</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#3">Chapter 3. Expressions</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<p><h2>3.3 &ndash; Logical Operators</h2>
The logical operators are
<b>and</b>, <b>or</b>, and <b>not</b>.
Like control structures, all logical operators
consider <B>false</B> and <B>nil</B> as false and anything else as true.
The operator <b>and</b> returns its first argument if it is false;
otherwise, it returns its second argument.
The operator <b>or</b> returns its first argument
if it is not false;
otherwise, it returns its second argument:
<pre>
    print(4 and 5)         --> 5
    print(nil and 13)      --> nil
    print(false and 13)    --> false
    print(4 or 5)          --> 4
    print(false or 5)      --> 5
</pre>
Both <b>and</b> and <b>or</b> use short-cut evaluation,
that is, they evaluate their second operand only when necessary.

<p>A useful Lua idiom is <code>x = x or v</code>,
which is equivalent to
<pre>
    if not x then x = v end
</pre>
i.e., it sets <code>x</code> to a default value <code>v</code> when
<code>x</code> is not set
(provided that <code>x</code> is not set to <B>false</B>).

<p>Another useful idiom is <code>(a and b) or c</code>
(or simply <code>a and b or c</code>,
because <b>and</b> has a higher precedence than <b>or</b>),
which is equivalent to the C expression
<pre>
    a ? b : c
</pre>
provided that <code>b</code> is not false.
For instance, we can select the maximum of two numbers
<code>x</code> and <code>y</code> with a statement like
<pre>
    max = (x > y) and x or y
</pre>
When <code>x > y</code>, the first expression of the <b>and</b> is true,
so the <b>and</b> results in its second expression (<code>x</code>)
(which is also true, because it is a number),
and then the <b>or</b> expression results in the value of
its first expression, <code>x</code>.
When <code>x > y</code> is false, the <b>and</b> expression is false
and so the <b>or</b> results in its second expression, <code>y</code>.

<p>The operator <b>not</b> always returns <B>true</B> or <B>false</B>:
<pre>
    print(not nil)      --> true
    print(not false)    --> true
    print(not 0)        --> false
    print(not not nil)  --> false
</pre>

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="3.4.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

